package se.webbzon.boltzmann.menu;

import java.awt.Image;
import java.awt.image.ImageObserver;

import se.webbzon.oschi01.worldcamera.WorldCameraPane;

public abstract class MSlider extends MItem {
	
	// The layout of the game menu slider
	private final SliderLayout layout;
	
	// The value of this game menu slider (0.0-1.0).
	private volatile double value;
	
	// Whatever this game menu slider is pressed.
	private boolean pressed;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public MSlider(ImageObserver observer, SliderLayout layout, double value) {
		super(0,0,0,-1,-1);
		this.layout = layout;
		this.value = value;
		pressed = false;
		setWidth(	layout.getBackgroundImage().get(0).getWidth(observer) + 
					layout.getSliderImage().get(0).getWidth(observer));
		setHeight(Math.max(	layout.getBackgroundImage().get(0).getHeight(observer), 
							layout.getSliderImage().get(0).getHeight(observer)));
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** This method is invoked when the value of the 
	 * game menu slider is changed. **/
	public abstract void onSetValue(double value);
	
	/** Sets the value of this game menu slider. **/
	public void setValue(double value) {
		if (value != this.value) {
			this.value = value;
			onSetValue(value);
		}
	}
	
	/** Returns the value of this game menu slider. **/
	public double getValue() {
		return value;
	}

	@Override public void onDraw(GameMenu menu, WorldCameraPane hud) {
		int index = hasFocus() ? (pressed ? 2 : 1): 0;
		
		Image background = layout.getBackgroundImage().get(0);
		Image slider = layout.getSliderImage().get(index);
		int bw = layout.getBackgroundImage().get(0).getWidth(hud.getImageObserver());
		int bh = layout.getBackgroundImage().get(0).getHeight(hud.getImageObserver());
		int sw = layout.getSliderImage().get(0).getWidth(hud.getImageObserver());
		int sh = layout.getSliderImage().get(0).getHeight(hud.getImageObserver());
		
		
		hud.drawImage(background, getX() + sw/2 , getY() + (getHeight() - bh)/2);
		hud.drawImage(	slider, 
						getX() + (int) (value * bw), 
						getY() + (getHeight() - sh)/2);
	}

	@Override public void onPress(GameMenu menu, int x, int y) {
		if (isWithin(x,y))
			pressed = true;
	}

	@Override public void onRelease(GameMenu menu, int x, int y) {
		pressed = false;
	}

	@Override public boolean isDraggable() {
		return true;
	}

	@Override public void onDrag(GameMenu menu, int x, int y) {
		if (pressed) {
			double v = Math.max(0, Math.min(1, (x - getX()) / (double) getWidth()));
			if (v != value) {
				value = v;
				onSetValue(v);
			}
		}
	}
	
	@Override public int getDragX() {
		return getX() + (int) (value * getWidth());
	}

	@Override public boolean isFocusable() {
		return true;
	}

}
